<?php
/**
 * 产品控制器
 *
 * @category     Shuxun Cms
 * @date         2015年10月18日
 * @author       Shuxun Team <kaiping.jiang@scshux.com>
 * @file         ProductController.php
 */
namespace Scshux\Front\Controllers;
use Scshux\Utils\JSON;
use Scshux\Mvc\SModel;
use Scshux\Utils\Uploader;
use Scshux\Mvc\SQuery;
use Scshux\Plugin\Wechat;
use Scshux\Utils\Helper;
use Scshux\Tag\SmartyTag;
use Scshux\Utils\Tree;
use Scshux\Utils\Format;
class ProductController extends  ControllerBase
{
	/**
	 * 产品列表
	 */
	public  function  indexAction()
	{
		$cid = intval($_REQUEST['cid']);
		$page = intval($_REQUEST['_page']);
		if(empty($page))$page =1;
		//新增语言过滤
		$site_id = $this->getCore()->getSiteID();
		//查找顶层分类
		
		//做一个获取所有产品的标记
		$bj = 0 ;
		if($site_id ==2){
			$parentID = 45;
			if(!$cid) $cid = 53;
		}else{
			$parentID = 0;
			if(!$cid)
			{
				$bj = 1 ;
			}
		}

		//读取当前分类
		$goodsCategoryModel = new SModel('category');
		
		//读取所有分类
		$goodsCategoryModel->setFetchMode(\PDO::FETCH_ASSOC);
		$goodsCategories = $goodsCategoryModel->query('site_id='.$site_id,'*','sort desc,id','desc');
		$treeObj = new Tree($goodsCategories);

		//读取当前语言下所有的分类
		$childCategories = $treeObj->getTree('child',$parentID);
		
		/**
		 * 读取当前id的分类
		 */
		if($bj == 1)
		{
			$childIds = $treeObj->getChilds(0);
		}
		else 
		{
			$childIds = $treeObj->getChilds($cid);
			$childIds[] = $cid;
		}
		
		//读取产品
		$goodsQuery = new SQuery('goods as g');
		$goodsQuery->join = ' join category as c on g.cat_id=c.id ';
		$goodsQuery->fields = 'g.goods_id,g.cat_id,g.goods_name,g.goods_img,c.name as cat_name';
		$goodsQuery->where = 'g.cat_id in('.join(',', $childIds).')' .' and g.site_id='.$site_id;;
		$goodsQuery->page = $page;
		$goodsQuery->pagesize = 16;
		$goodsItems = $goodsQuery->find();
		
		//标题长度限制
		if($goodsItems)
		{
			foreach ($goodsItems as &$good)
			{
				$good->goods_name = Format::cut_str($good->goods_name, 4,'') ;
			}
		}
		
		$this->view->setVar('pager', $goodsQuery->paging->getPagers());
		$this->view->setVar('goodsItems', $goodsItems);
		$this->view->setVar('currentCid',$cid);
		$this->view->setVar('articles', $this->getArticleList());

	}

	/**
	 * 产品展示
	 */
	public  function  showAction()
	{
		$id = $this->request->get('id','int',0);
		if(empty($id)){
			$this->redirect('');
		}
		$id = intval($id);
		//新增语言过滤
		$site_id = $this->getCore()->getSiteID();

		//查找顶层分类
		if($site_id ==2){
			$parentID = 45;
			if(!$cid) $cid = 53;
		}else{
			$parentID = 0;
			if(!$cid) $cid = 53;
		}
		
		//读取产品
		$goodsQuery = new SQuery('goods as g');
		$goodsQuery->join = ' join category as c on g.cat_id=c.id ';
		$goodsQuery->fields = 'g.*,c.name as cat_name';
		$goodsQuery->where = 'g.goods_id='.$id .' and g.site_id='.$site_id;

		$goodsItems = current($goodsQuery->find());

		if(empty($goodsItems)){
			$this->redirect('');
		}

		//读取所有分类
		$goodsCategoryModel = new SModel('category');
		$goodsCategoryModel->setFetchMode(\PDO::FETCH_ASSOC);
		$goodsCategories = $goodsCategoryModel->query('site_id='.$site_id,'*','sort desc,id','desc');
		$treeObj = new Tree($goodsCategories);

		$childCategories = $treeObj->getTree('child',$parentID);

		$childIds = $treeObj->getChilds($goodsItems->cat_id);
		$childIds[] = intval($goodsItems->cat_id);

		//读取图片
		$goodsGalleryModel = new SModel('goods_gallery');
		$goodsGalleries = $goodsGalleryModel->query('goods_id='.$id);
		//更新次数
		$goodsModel = new SModel('goods');
		$goodsModel->updateBySql('goods_id='.$id, 'click_count = click_count+1');

		//读取上一篇
		$goodsModel = new SModel('goods');
		$preGoodsItem = $goodsModel->query('cat_id='.$goodsItems->cat_id.' and goods_id<'.$id . ' and site_id='.$site_id,'goods_id,goods_name','goods_id','desc',1);
		$preGoodsItem = current($preGoodsItem);
		
		//读取下一篇
		$nextGoodsItem = $goodsModel->query('cat_id='.$goodsItems->cat_id.' and goods_id>'.$id. ' and site_id='.$site_id,'goods_id,goods_name','goods_id','asc',1);
		$nextGoodsItem = current($nextGoodsItem);
		
		$this->view->setVar('categories', $childCategories);
		$this->view->setVar('goodsGalleries', $goodsGalleries);
		$this->view->setVar('item', $goodsItems);
		$this->view->setVar('currentCid',$cid);
		$this->view->setVar('articles', $this->getArticleList());
		$this->view->setVar('nextGoodsItem', $nextGoodsItem);
		$this->view->setVar('preGoodsItem', $preGoodsItem);
	}

	
	/**
	 * 案例列表
	 */
	public function caseAction()
	{
	    $zxstyleModel = new SModel('goods_style');
	    $zxStyle = $zxstyleModel->query(false,'*','id');
	     
	    $zxsizeModel = new SModel('goods_roomsize');
	    $zxsize = $zxsizeModel->query(false,'*','id');
	     
	    $zxareaModel = new SModel('goods_roomarea');
	    $zxarea = $zxareaModel->query(false,'*','id');
	     
	    $zxpriceModel = new SModel('goods_roomprice');
	    $zxprice = $zxpriceModel->query(false,'*','id');
	    
	    $page = intval($_REQUEST['_page']);
	    
	    $cid = intval($_REQUEST['cid']);
	    
	    $style = intval($_REQUEST['style']);
	    $size = intval($_REQUEST['size']);
	    $area = intval($_REQUEST['area']);
	    $price = intval($_REQUEST['price']);
	    
	    $where = ' 1=1 ';
	    if ($cid)
	    {
	        $where .= ' and cs.cat_id='.$cid;
	    }
	    if ($style)
	    {
	        $where .= ' and cs.style='.$style;
	    }
	    if ($size)
	    {
	        $where .= ' and cs.size='.$size;
	    }
	    if ($area)
	    {
	        $where .= ' and cs.area_group='.$area;
	    }
	    if ($price)
	    {
	        $where .= ' and cs.price_group='.$price;
	    }
	    
	    
	    $casesQuery = new SQuery('cases as cs');
	    $casesQuery->fields = 'cs.cat_id,cs.id,cs.name,cs.area,cs.img,s.style_name,rs.name as rsname,cs.price';
	    $casesQuery->where = $where;
	    $casesQuery->join = ' left join goods_style as s on s.id=cs.style left join goods_roomsize as rs on rs.id=cs.size ';
	    $casesQuery->order = 'cs.id desc';
	    $casesQuery->limit = 4;
	    $casesQuery->page = $page;
	    $casesQuery->pagesize = 16;
	    
	    $cases = $casesQuery->find();
	    
	    $this->view->setVar('zxstyle',$zxStyle);
	    $this->view->setVar('zxsize',$zxsize);
	    $this->view->setVar('zxarea',$zxarea);
	    $this->view->setVar('zxprice',$zxprice);
	    
	    $this->view->setVar('pager', $casesQuery->paging->getPager());
	    $this->view->setVar('items', $cases);
	    
	    $this->view->setVar('cid', $cid);
	    $this->view->setVar('style', $style);
	    $this->view->setVar('size', $size);
	    $this->view->setVar('area', $area);
	    $this->view->setVar('price', $price);
	}
	
	/**
	 * 装修案例详情
	 */
	public function caseshowAction()
	{
	    $casesQuery = new SQuery('cases as cs');
	    $casesQuery->fields = 'cs.id,cs.name,cs.area,cs.img,s.style_name,de.name as dename';
	    $casesQuery->join = ' left join goods_style as s on s.id=cs.style left join designer as de on de.id=cs.design_id ';
	    $casesQuery->order = 'cs.id desc';
	    $casesQuery->limit = 4;
	    $cases = $casesQuery->find();
	    
	    $caseModel = new SModel('cases');
	    $id = intval($_REQUEST['id']);
	    
	    $case = $caseModel->getObj('id='.$id);
	    
	    if (!$case)
	    {
	        return $this->redirect('errors/err404');
	    }
	    
	    $gstyleModel = new SModel('goods_style');
	    $gstyle = $gstyleModel->getObj('id='.$case->style,'style_name');
	    
	    $case->styleName = $gstyle->style_name;
	    
	    $designModel = new SModel('designer');
	    $design = $designModel->getObj('id='.$case->design_id,'name');
	    
	    $case->designName = $design->name;
	    
	    $sizeModel = new SModel('goods_roomsize');
	    $size = $sizeModel->getObj('id='.$case->size,'name');
	     
	    $case->roomsize = $size->name;
	    
	    //读取相册图片
	    $goodsGalleryModel = new SModel('goods_gallery');
	    $goodsGalleries = $goodsGalleryModel->query('goods_id='.$id);
	    
	    $this->view->setVar('case', $case);
	    
	    $this->view->setVar('items', $cases);
	    
	    $this->view->setVar('gallery', $goodsGalleries);
	    
	}
	
	/**
	 * 生成
	 */
	
}